<?php
namespace Admin\Controller;
use Admin\Controller\CommonController;
class JueseController extends CommonController {

	public function index() {
		$M=new \Think\Model("auth_group");
		$count = $M->count();
		$page =new \Think\Page($count,15);
		$showPage = $page->show();
			$show=$M->limit($Page->firstRow, $Page->listRows)->select();
		$this->assign("list",$show);

		$this->assign("page", $showPage);
		$this->assign("pageCount",$count);
		$this->display();
	}
	
	
		
	/**
	 +----------------------------------------------------------
	 * 添加管理员
	 +----------------------------------------------------------
	 */
	public function addAdmin() {
		if (IS_POST) {
		      $data['title']=I('post.title');
		      $data['rules']='1,2';
		        if(M('auth_group')->add($data)){
                // $this->success('添加角色成功',U('Juese/index'));
                 $this->redirect('Juese/index');
                }
		} else {
			$this->assign('navAction','添加角色');
			$this->display();
		}
	}
	
		public function xiugai() {
		if (IS_POST) {
			  $id=I('post.id');
		      $data['title']=I('post.title');
		        if(M('auth_group')->where('id='.$id)->setfield($data)){
                //$this->success('修改成功',U('Juese/index'));
                $this->redirect('Juese/index');
                }else{ 
                 $this->success('未修改',U('Juese/index'));}
		} else {
			$id=I('get.id');
			$xiugai=M('auth_group')->where('id='.$id)->find();
            $this->assign('data',$xiugai);
			$this->display();
		}
	}
	

	/**
	 +----------------------------------------------------------
	 * 修改密码
	 +----------------------------------------------------------
	 */
	public function editPass() {
		if (IS_POST) {
			$this->checkToken();
			header('Content-Type:application/json; charset=utf-8');
			if(D("Access")->editPwd()){
				$this->success('修改成功',U('Access/editPass'));
			}else{
				$this->error(D("Access")->getError(),U('Access/editPass'));
			}
		} else {
			$this->assign('navAction','修改密码');
			$this->display();
		}
	}
	
	
	/**
	 +----------------------------------------------------------
	 * 修改 管理员
	 +----------------------------------------------------------
	 */
	public function editAdmin() {
		if (IS_POST) {
			$this->checkToken();
			header('Content-Type:application/json; charset=utf-8');
			    $data=I('post.');
            $map=array(
                'id'=>$data['id']
                );
            $data['rules']=implode(',', $data['rule_ids']);
            $result=M('auth_group')->where($map)->save($data);
            if ($result) {
                //$this->success('操作成功',U('Admin/Juese/index'));
                 $this->redirect('Admin/Juese/index');
            }else{
                $this->error('操作失败');
            }
		} else {
	
           $rule_data=M('auth_rule')->where('pid=0')->order('id asc')->select();
        foreach ($rule_data as &$value) {
                   $erji=M('auth_rule')->where('pid='.$value['id'])->select();
                   $value['erji']=$erji;
        }
        
      
			$M = M("auth_group");
			$id = (int) $_GET['id'];
			 $group_data = $M->where("`id`=" . $id)->find();
			  $group_data['rules']=explode(',', $group_data['rules']);
	
			// if ($info['id'] == 1) {
			// 	 $this->error('超级管理员信息不允许操作'); 
			// }
				$this->assign("rule_data", $rule_data);
			$this->assign("group_data", $group_data);
			$this->assign('navAction','修改管理员');
			$this->display();
		}
	}
	
	
	
	
	public function delAdmin(){

		$ids = is_numeric($_GET['aid']) ? array($_GET['aid']) : $_POST['aid'];
		$str="('".implode("','",$ids)."')";
		if (M("auth_group")->where("id in ".$str)->delete()) {
		
			echo json_encode(array("status"=>1,"info"=>"成功删除"));
		} else {
			echo json_encode(array("status"=>0,"info"=>"删除失败，可能是不存在该ID的记录"));
		}
	}
	
    		
}

    
  


?>